﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Manon.Autofac;
using Manon.ConfigCenter.Web.ApplicationCore.Dtos.Environment;
using Manon.ConfigCenter.Web.ApplicationCore.Interfaces;
using Manon.Core.Commons;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Manon.ConfigCenter.Web.Controllers
{
    public class EnvironmentController : Controller
    {
        private readonly ILogger<EnvironmentController> _logger;

        public EnvironmentController(ILogger<EnvironmentController> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 编辑页
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit()
        {
            return View();
        }

        /// <summary>
        /// 密钥设置
        /// </summary>
        /// <returns></returns>
        public IActionResult Secret()
        {
            return View();
        }


        [Authorize]
        [HttpGet]
        public async Task<List<EnvironmentOutput>> GetEnvironmentList()
        {
            var environmentService = Resolver.Resolve<IEnvironmentService>();
            var result = await environmentService.GetEnvironmentListAsync();

            return result;
        }
        [Authorize]
        [HttpGet]
        public async Task<List<EnvironmentSimpleOutput>> GetEnvironmentSimpleList()
        {
            var environmentService = Resolver.Resolve<IEnvironmentService>();
            var result = await environmentService.GetEnvironmentSimpleListAsync();

            return result;
        }

        [Authorize]
        [HttpGet]
        public async Task<EnvironmentOutput> GetEnvironmentOutput(int EnvId)
        {
            var environmentService = Resolver.Resolve<IEnvironmentService>();
            var result = await environmentService.GetEnvironmentOutput(EnvId);

            return result;
        }
        [Authorize]
        [HttpPost]
        public async Task<Result> SaveEnvironment([FromBody] SaveEnvironmentInput Input)
        {
            var environmentService = Resolver.Resolve<IEnvironmentService>();
            var result = await environmentService.SaveEnvironment(Input);

            return result;
        }

        [Authorize]
        [HttpPost]
        public async Task<Result> DelEnvironment([FromBody] DelEnvironmentInput Input)
        {
            var environmentService = Resolver.Resolve<IEnvironmentService>();
            var result = await environmentService.DelEnvironment(Input.EnvId);

            return result;
        }
    }

}
